package xyz.kongbai121.modules.sys.controller;

import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import xyz.kongbai121.common.utils.PageUtils;
import xyz.kongbai121.common.utils.Result;
import xyz.kongbai121.modules.sys.entity.FileUploadLocalEntity;
import xyz.kongbai121.modules.sys.service.FileUploadLocalService;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;


/**
 * 文件上传(保存到本机)
 *
 * @author zbn
 * @date 2021-11-29 13:36:08
 */
@Api(tags = "文件上传(保存到本机)")
@RestController
@RequestMapping("/sys/fileuploadlocal")
public class FileUploadLocalController {
    @Autowired
    private FileUploadLocalService fileUploadLocalService;


    /**
     * 列表
     */
    @ApiOperation(value = "列表")
    @GetMapping("/list")
    @RequiresPermissions("sys:fileuploadlocal:list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页码", paramType = "query", dataType = "long", required=true),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数", paramType = "query", dataType = "long", required=true),
    })
    public Result<PageUtils> list(@RequestParam(value = "curPage") Long curPage,
                                  @RequestParam(value = "pageSize") Long pageSize){

        Map<String, Object> params = new HashMap<>();
        params.put("page", curPage);
        params.put("limit", pageSize);
        PageUtils page = fileUploadLocalService.queryPage(params);

        return Result.ok(page);
    }


    /**
     * 详情
     */
    @ApiOperation(value = "详情")
    @GetMapping("/info/{id}")
    @RequiresPermissions("sys:fileuploadlocal:info")
    public Result<FileUploadLocalEntity> info(@ApiParam(value = "id", example = "0", required=true) @PathVariable("id") Long id){
		FileUploadLocalEntity fileUploadLocal = fileUploadLocalService.getById(id);

        return Result.ok(fileUploadLocal);
    }

    /**
     * 保存
     */
    @ApiOperation(value = "文件上传(本地)")
    @PostMapping("/save")
    @RequiresPermissions("sys:fileuploadlocal:save")
    public Result<?> save(@ApiParam(value = "文件", required=true) @RequestParam("file") MultipartFile file) throws IOException {
        FileUploadLocalEntity fileUploadLocal = fileUploadLocalService.uploadAndSave(file);
        return Result.ok(fileUploadLocal);
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除")
    @PostMapping("/delete")
    @RequiresPermissions("sys:fileuploadlocal:delete")
    public Result<?> delete(@ApiParam(value = "id数组", required=true) @RequestBody Long[] ids){
		fileUploadLocalService.removeByIds(Arrays.asList(ids));

        return Result.ok();
    }

}
